package cn.cosmosx.base.security.exception;

import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import cn.cosmosx.base.constant.ResultCode;
import cn.cosmosx.base.util.ResponseUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 自定义未登录认证异常处理器
 */
@Slf4j
public class CustomAuthenticationEntryPoint implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException {
        log.error("{}#未登录认证异常:{}", this.getClass().getName(), authException.getMessage());
        ResponseUtils.wrapResponse(response, ResultCode.U00001, authException.getMessage());
    }
}
